package com.cs4261.smartalarm.backend;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {
	//database name and version
	private static final String DATABASE_NAME = "smartalarm.db";
	private static final int DATABASE_VERSION = 1;
	
	//alarms table
	public static final String TABLE_ALARMS = "alarms";
	//alarm information: long id, string name, int (1|0)
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NAME = "_name";
	public static final String COLUMN_ACTIVE = "_active";
	//time information: int [1, 24], int [0-59]
	public static final String COLUMN_HOUR = "_hour";
	public static final String COLUMN_MINUTE = "_minute";
	//day information: int (1|0)...
	public static final String COLUMN_REPEAT = "_repeat";
	public static final String COLUMN_SUNDAY = "_sunday";
	public static final String COLUMN_MONDAY = "_monday";
	public static final String COLUMN_TUESDAY = "_tuesday";
	public static final String COLUMN_WEDNESDAY = "_wednesday";
	public static final String COLUMN_THURSDAY = "_thursday";
	public static final String COLUMN_FRIDAY = "_friday";
	public static final String COLUMN_SATURDAY = "_saturday";
	//weather info: int (1|0), int offset...
	public static final String COLUMN_WEATHER = "_weather"; //keep?
	public static final String COLUMN_W1 = "_w1";
	public static final String COLUMN_W2 = "_w2";
	public static final String COLUMN_W3 = "_w3";
	public static final String COLUMN_W4 = "_w4";
	//Location information: int (1|0), long foreign id...
	public static final String COLUMN_TRAFFIC = "_traffic"; //keep?
	public static final String COLUMN_LOCATION1 = "_location1";
	public static final String COLUMN_LOCATION2 = "_location2";
	//Snooze duration
	public static final String COLUMN_SNOOZE = "_snooze";
	//Alarm tone
	public static final String COLUMN_TONE = "_tone";
	
	
	
	//locations table
	public static final String TABLE_LOCATIONS = "locations";
	//public static final String COLUMN_ID = "_id";
	//public static final String COLUMN_NAME = "_name";
	public static final String COLUMN_ADDRESS = "_address";
	
	//other tables?

	//Table creation sql statements
	private static final String CREATE_ALARMS = 
			"create table " + TABLE_ALARMS + "("
			+ COLUMN_ID	+ " integer primary key autoincrement, "
			+ COLUMN_NAME + " text not null, "
			+ COLUMN_ACTIVE + " integer not null, "
			+ COLUMN_HOUR + " integer not null, "
			+ COLUMN_MINUTE + " integer not null, "
			+ COLUMN_REPEAT + " integer not null, "
			+ COLUMN_SUNDAY + " integer not null, "
			+ COLUMN_MONDAY + " integer not null, "
			+ COLUMN_TUESDAY + " integer not null, "
			+ COLUMN_WEDNESDAY + " integer not null, " 
			+ COLUMN_THURSDAY + " integer not null, " 
			+ COLUMN_FRIDAY + " integer not null, " 
			+ COLUMN_SATURDAY + " integer not null, "
			+ COLUMN_WEATHER + " integer not null, " 
			+ COLUMN_W1 + " integer not null, " 
			+ COLUMN_W2 + " integer not null, " 
			+ COLUMN_W3 + " integer not null, "
			+ COLUMN_W4 + " integer not null, " 
			+ COLUMN_TRAFFIC + " integer not null, " 
			+ COLUMN_LOCATION1 + " integer, " 
			+ COLUMN_LOCATION2 + " integer, "
			+ COLUMN_SNOOZE + " integer, "
			+ COLUMN_TONE + " text not null"
			+ ");";
	
	private static final String CREATE_LOCATIONS = 
			"create table "	+ TABLE_LOCATIONS + "(" 
			+ COLUMN_ID	+ " integer primary key autoincrement, " 
			+ COLUMN_NAME + " text not null," 
			+ COLUMN_ADDRESS + " text not null);";
	

	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_ALARMS);
		db.execSQL(CREATE_LOCATIONS);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(MySQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALARMS);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS);
		onCreate(db);
	}

}